#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "RingBuffer.hpp"

using namespace RingBufferModule;

void* Productor(void* args)
{
    RingBuffer<int>* rb = static_cast<RingBuffer<int>*>(args);
    int data = 1;
    while (true)
    {
        // sleep(1);
        rb->Equeue(data);
        printf("生产了一个数据: %d\n", data);
        data++;
    }
}

void* Consumer(void* args)
{
    RingBuffer<int>* rb = static_cast<RingBuffer<int>*>(args);
    while (true)
    {
        // sleep(1);
        int data;
        rb->Pop(&data);
        printf("消费了一个数据: %d\n", data);
    }
}

int main()
{
    RingBuffer<int>* rb = new RingBuffer<int>(5);
    pthread_t p1, p2, p3, c1, c2;
    pthread_create(&p1, nullptr, Productor, rb);
    pthread_create(&p2, nullptr, Productor, rb);
    pthread_create(&p3, nullptr, Productor, rb);
    pthread_create(&c1, nullptr, Consumer, rb);
    pthread_create(&c2, nullptr, Consumer, rb);

    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    delete rb;
    return 0;
}
